package com.bootmy.controller;

import com.bootmy.Service.DictService;
import com.bootmy.config.JsonResult;
import com.bootmy.mapper.DictMapper;
import com.bootmy.model.Dict;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.*;

@RequestMapping("/Dict")
@Controller
public class DictController {

    @Resource
    public DictService dictService;
    @Resource
    public DictMapper dictMapper;

    /**
     * 根据ID查询对应的数据字典
     * @param id
     * @return
     */
    @ApiOperation(value="获取数据字典详细信息", notes="根据url的id来获取数据字典详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping(value = "find/{id}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){
        JsonResult r = new JsonResult();
        try {
            Dict dict = dictService.findUserById(Long.valueOf(id));
            r.setResult(dict);
            r.setStatus("成功");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("异常");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }
    /**
     * 查询数据字典列表
     * @return
     */
    @ApiOperation(value="获取数据字典列表", notes="获取数组字典列表")
    @RequestMapping(value = "findAll", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getUserList (){
        JsonResult r = new JsonResult();
        try {
            List<Dict> dictList = dictService.findAllUser();
            r.setResult(dictList);
            r.setStatus("成功");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("异常");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }
    /**
     * 添加数据字典
     * @return
     */
    @ApiOperation(value="创建数据字典", notes="根据Dict对象创建数据字典")
    @ApiImplicitParam(name = "dict", value = "数据字典详细实体Dict", required = true, dataType = "Dict")
    @RequestMapping(value = "dict", method = RequestMethod.POST)
    public ResponseEntity<JsonResult> add (@RequestBody Dict dict){
        JsonResult r = new JsonResult();
        try {
            dictMapper.insert(dict);
            r.setResult(dict.getId());
            r.setStatus("成功");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("异常");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }
    /**
     * 根据id删除对应的数据字典
     * @param id
     * @return
     */
    @ApiOperation(value="删除数据字典", notes="根据url的id来指定删除数据字典")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "deleteById/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<JsonResult> delete (@PathVariable(value = "id") Integer id){
        JsonResult r = new JsonResult();
        try {
            dictMapper.deleteByPrimaryKey(Long.valueOf(id));
            r.setResult(id);
            r.setStatus("成功");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("异常");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }
    /**
     * 根据id修改数据字典信息
     * @param dict
     * @return
     */
    @ApiOperation(value="更新", notes="根据url的id来指定更新数据字典")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典ID", required = true, dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "dict", value = "用户实体dict", required = true, dataType = "Dict")
    })
    @RequestMapping(value = "update/{id}", method = RequestMethod.PUT)
    public ResponseEntity<JsonResult> update (@PathVariable("id") Long id, @RequestBody Dict dict){
        JsonResult r = new JsonResult();
        try {

            Dict d = new Dict();
            d.setId(id);
            d.setDictName(dict.getDictName());
            d.setDictCode(dict.getDictCode());
            d.setDictStatus(dict.getDictStatus());
            dictMapper.updateByPrimaryKeySelective(d);
            r.setResult(d);
            r.setStatus("成功");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("失败");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }
    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String  jsonTest() {
        return " hi you!";
    }
}
